<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class WithdrawService extends BaseService
{

    /**
     * 列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getList($pages, $uid, $status)
    {
        $status_text_arr = $this->withdrawStatus;
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbUserWithdraw)->where('del', 0)->where('uid', $uid);
        if (!empty($status)) {
            $db = $db->where('status', $status);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get()
            ->map(function ($value){
                return (array)$value;
            })
            ->toArray();
        foreach ($res as &$item) {
            $item['status_name'] = !empty($status_text_arr[$item['status']]) ? $status_text_arr[$item['status']] : '';
        }
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有数据', 'data' => [], 'total' => 0);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res, 'total' => $total);
    }

    /*
     * 添加账户
     * */
    public function addInfo($uid, $pas) {
        $pas['uid'] = $uid;
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = DB::table($this->dbUserWithdrawInfo)->insertGetId($pas);
        if (!$lastid) {
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 修改
     * @param int $id 账户ID
     * @param array $pas 请求参数
     */
    public function modifyInfo($uid, $pas)
    {
        $res = (array) DB::table($this->dbUserWithdrawInfo)->where('del', 0)->where('uid', $uid)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '账户不存在');
        }
        $pas['uptime'] = toDate();
        $up = DB::table($this->dbUserWithdrawInfo)->where('uid', $uid)->update($pas);
        if ($up == false){
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 详情
     * @param int $uid
     */
    public function getInfo($uid)
    {
        $res = (array) DB::table($this->dbUserWithdrawInfo)->where('del', 0)
            ->where('uid', $uid)
            ->first();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '账户不存在', 'data' => []);
        }
        $txOne = (array) $this->dbConn($this->dbTixian)
            ->where('uid', $uid)
            ->first();
        $res['tixian'] = empty($txOne) ? 0 : toNumber($txOne['amount']);
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /*
     * 添加提现记录
     * */
    public function add($user, $amount) {
        $amount = $amount * 100;
        $pas['uid'] = $user['id'];
        $pas['addtime'] = $pas['uptime'] = toDate();
        $pas['amount'] = $amount;
        DB::beginTransaction(); //开启事务
        try {
            //$this->dbConn($this->dbUserIncome)->where('')->update();
            DB::table($this->dbUserWithdraw)->insertGetId($pas);
            DB::table($this->dbUser)->where('id', $user['id'])->increment('income_freeze', $amount);
            DB::table($this->dbUser)->where('id', $user['id'])->decrement('income', $amount);
            DB::commit(); //提交事务
            return array('code' => 0, 'msg' => 'success');
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return array('code' => -1, 'msg' => '操作失败');
        }
    }
}
